package com.wulinbus.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.fancy.common.utils.JsonBinder;
import com.wulinbus.dao.cache.RedisFactory;
import com.wulinbus.dao.cache.RedisFactory.Redis;
import com.wulinbus.dao.cache.RedisKeyFactory;
import com.wulinbus.dao.user.UserDAO;
import com.wulinbus.model.UserModel;
import com.wulinbus.service.UserService;

@Service
public class UserServiceImpl implements UserService {

	@Resource
	private UserDAO userDAO;

	private static final Redis cache = RedisFactory.getRedis();

	@Override
	public Integer updateUser(UserModel user) {
		return userDAO.updateUser(user);
	}


	@Override
	public void delete(UserModel userModel) {
		userDAO.delete(userModel);
	}

	@Override
	public List<UserModel> getList() {
		// return getListByCache2();
		return userDAO.getList(null,null);
	}

	/**
	 * 演示缓存的使用方法-把object转化为string后写入或读取
	 * 
	 * @return
	 */
	public List<UserModel> getListByCache() {

		final String key = RedisKeyFactory.getCacheKey();
		List<UserModel> list = new ArrayList<UserModel>();
		String userJson = cache.get(key);
		if (StringUtils.isNotBlank(userJson)) {
			list = JsonBinder.fromJson(userJson, list.getClass());
		} else {
			list = getList();
			cache.set(key, JsonBinder.toJson(list), 60 * 60 * 1);// 缓存1小时
		}
		//
		return list;
	}

	/**
	 * 演示缓存的使用方法-直接写入或读取object
	 * 
	 * @return
	 */
	public List<UserModel> getListByCache2() {

		final String key = RedisKeyFactory.getCacheKey();
		List<UserModel> list = new ArrayList<UserModel>();
		list = (List<UserModel>) cache.getObject(key);
		if (list == null) {
			list = getList();
			cache.setObject(key, list);// 缓存1小时
		}
		//
		return list;
	}

	@Override
	public UserModel login(Map<String, Object> param) {
		// TODO Auto-generated method stub
		return userDAO.login(param);
	}
	public UserModel adminLogin(Map<String, Object> param){
		return userDAO.adminLogin(param);
	}
	@Override
	public Integer getMaxId() {
		// TODO Auto-generated method stub
		return userDAO.getMaxId();
	}

	public void insertCompUser(UserModel userModel, Map<String, Object> param) {
		// 新增企业用户
		userDAO.insertCompUser(userModel);
		String roleList = (String) param.get("roleList");
		if (!"".equals(roleList))
		// 新增用户角色
		{
			userDAO.insertUserRole(param);
		}

	}

	public void insertPerUser(UserModel userModel, Map<String, Object> param) {
		// 新增用户角色
		String roleList = (String) param.get("roleList");
		if (!"".equals(roleList)) {
			// 新增个人用户
			userDAO.insertPerUser(userModel);
		}
	}

	@Override
	public void addCompUser(UserModel userModel) 
	{
			userDAO.addCompUser(userModel);
	}
	@Override
	public void addCreative(UserModel userModel)
	{
			userDAO.addCreative(userModel);
	}

	@Override
	public UserModel findUserByEmail(String email) {
		// TODO Auto-generated method stub
		return userDAO.findUserByEmail(email);
	}

	@Override
	public UserModel creativeExist(String title) {
		// TODO Auto-generated method stub
		return userDAO.creativeExist(title);
	}


	@Override
	public long getMaxCid() {
		// TODO Auto-generated method stub
		return userDAO.getMaxCid();
	}

	@Override
	public void addCoverUrl(UserModel userModel) {
		// TODO Auto-generated method stub
		userDAO.addCoverUrl(userModel);
	}

	@Override
	public void addMatterUrl(UserModel userModel) {
		// TODO Auto-generated method stub
		userDAO.addMatterUrl(userModel);
	}

	@Override
	public Integer findUidByEmail(String email) {
		// TODO Auto-generated method stub
		return userDAO.findUidByEmail(email);
	}

	@Override
	public void addCreativeUrl(UserModel userModel_C) {
		// TODO Auto-generated method stub
		userDAO.addCreativeUrl(userModel_C);
	}

	@Override
	public UserModel findUserByComp(String comp) {
		// TODO Auto-generated method stub
		return userDAO.findUserByComp(comp);
	}

	@Override
	public UserModel findUserByUid(Integer uid) {
		// TODO Auto-generated method stub
		return userDAO.findUserByUid(uid);
	}


	@Override
	public String findCompByUid(Integer uid) {
		// TODO Auto-generated method stub
		return userDAO.findCompByUid(uid);
	}

	@Override
	public List<UserModel> getListByUid(List<Integer> uids) {
		return userDAO.getListByUid(uids);
	}

	@Override
	public List<Integer> findUidByKeyAuthor(String search) {
		// TODO Auto-generated method stub
		return userDAO.findUidByKeyAuthor(search);
	}

	@Override
	public List<UserModel> findUserByKeyAuthor(String search) {
		// TODO Auto-generated method stub
		return userDAO.findUserByKeyAuthor(search);
	}

	@Override
	public void addLoginTime(Integer uid, String login_time) {
		// TODO Auto-generated method stub
		userDAO.addLoginTime(uid, login_time);
	}
	@Override
	public Integer getRoleByUid(Integer uid) {
		// TODO Auto-generated method stub
		return userDAO.getRoleByUid(uid);
	}
	
	@Override
	public Integer addBalance(Integer uid, Float money){
		return userDAO.addBalance(uid, money);
	}
	@Override
	public List<UserModel> getList(Integer start, Integer count){
		return userDAO.getList(start, count);
	}
	@Override
	public Integer getListCount(){
		return userDAO.getListCount();
	}

	@Override
	public void updateBalanceOfNumOne(Integer collect_budget,Integer sid) {
		// TODO Auto-generated method stub
		userDAO.updateBalanceOfNumOne(collect_budget,sid);
	}

	@Override
	public void updateBalanceOfCollector(Float collect_budget,Integer sid) {
		// TODO Auto-generated method stub
		userDAO.updateBalanceOfCollector(collect_budget,sid);
	}
}
